import { storageEnum } from '@/constant/index.js';

// 全屏
// eslint-disable-next-line no-shadow
export const fullscreen = (fullscreen) => {
  if (fullscreen) {
    const el = document.documentElement;
    const rfs = el.requestFullScreen || el.webkitRequestFullScreen
      || el.mozRequestFullScreen || el.msRequestFullScreen;
    rfs.call(el);
  } else {
    const el = document;
    const cfs = el.cancelFullScreen || el.webkitCancelFullScreen
      || el.mozCancelFullScreen || el.exitFullScreen;
    cfs.call(el);
  }
};

export const PLANT_STATION_ABBREVIATIONS = {
  仙女山站: '山',
  柏泉站: '泉',
  编钟站: '钟',
  奚贤站: '贤',
  长沙: '潇',
  奉节串补站: '盘',
  仙桃站: '桃',
  江夏站: '夏',
  江坪河: '坪',
  团林换流站: '林',
  吴都站: '都',
  白莲河厂: '莲',
  大吉站: '吉'
};

// 潮流图 厂站简称转换
export const translatePlantStationAbbreviation = (plantStation) => {
  if (plantStation) {
    if (PLANT_STATION_ABBREVIATIONS[plantStation]) {
      return PLANT_STATION_ABBREVIATIONS[plantStation];
    }
    return plantStation.slice(0, 1);
  }
  return plantStation;
};

export const uniq = (object) => {
  // eslint-disable-next-line no-undef
  if (!object || !Array.isArray(object)) {
    throw new Error('object不存在或object不是数组');
  }
  return [...new Set(object.map(JSON.stringify))].map(JSON.parse);
};

export const toFixed = (target, decimal = 0) => {
  if (!target) {
    console.error('小数值不存在');
    return '';
  }
  return Number(target).toFixed(decimal);
};

export const messageChannel = new MessageChannel();

export const verticalText = (str, rotate) => {
  if (rotate === 0) return str;
  let joinText = '';
  // eslint-disable-next-line no-restricted-syntax
  for (const el of str) {
    joinText += `${el}\n`;
  }
  return joinText;
};

export const getUserInfoStorage = () => JSON.parse(localStorage.getItem(storageEnum.USER_INFO) || '{}');
